<?php

namespace app\api\service;

use think\Config;
use think\Controller;
use app\common\Base;


class FengniaoService extends Controller
{
    private $app_id;
    private $app_secret;
    private $url;
    private $token;

    public function __construct()
    {
        $this->app_id = Config::get('fengniao.APP_ID');
        $this->app_secret = Config::get('fengniao.SECRET_KEY');
        $this->url = Config::get('fengniao.API_URL');
    }

    //接口通用请求
    public function makeRequest($data, $add_url)
    {
        $salt = mt_rand(1000, 9999);   // 生成随机数
        $url = $this->url . $add_url;

        $this->requestToken();
        $sig = $this->generateBusinessSign($this->app_id, $this->token, $data, $salt);   //生成签名
        $requestJson = json_encode(array(
            'app_id' => $this->app_id,
            'salt' => $salt,
            'data' => $data,
            'signature' => $sig
        ));
        $resp = $this->doPost($url, $requestJson);
        return $resp;
    }

    //get请求
    public static function doGet($url, $param = null)
    {
        if (!empty($param)) {
            $url = trim($url, '?') . '?' . http_build_query($param);
        }
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_AUTOREFERER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);     //  不进行ssl 认证
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
        $result = curl_exec($ch);
        $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);
        if (!empty($result) and $code == 200) {
            return $result;
        }
        return false;
    }

    //post请求
    public function doPost($url, $param, $method = "POST")
    {
        if (!empty($param) and is_array($param)) {
            $param = urldecode(json_encode($param));
        } else {
            $param = strval($param);
        }

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);     //  不进行ssl 认证

        if (strcmp($method, "POST") == 0) {  // POST 操作
            curl_setopt($ch, CURLOPT_POST, true);
        } else if (strcmp($method, "DELETE") == 0) { // DELETE操作
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
        }

        curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: Application/json'));
        $result = curl_exec($ch);
        $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);
        if (!empty($result) and $code == '200') {
            return $result;
        }
        return false;
    }

    // 生成签名
    public function generateSign($appId, $salt, $secretKey)
    {
        $seed = 'app_id=' . $appId . '&salt=' . $salt . '&secret_key=' . $secretKey;
        return md5(urlencode($seed));
    }

    //获取Token
    public function requestToken()
    {
        $salt = mt_rand(1000, 9999);  // 生成随机数
        $sig = $this->generateSign($this->app_id, $salt, $this->app_secret);   // 获取签名

        $url = $this->url . '/get_access_token';
        $tokenStr = $this->doGet($url, array('app_id' => $this->app_id, 'salt' => $salt, 'signature' => $sig));
        $this->token = json_decode($tokenStr, true)['data']['access_token'];
    }

    //生成带token签名
    public function generateBusinessSign($appId, $token, $urlencodeData, $salt)
    {
        $seed = 'app_id=' . $appId . '&access_token=' . $token
            . '&data=' . $urlencodeData . '&salt=' . $salt;
        return md5($seed);
    }

}
